// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
const { ipcRenderer } = require('electron')

const { contextBridge } = require('electron')

// 监听主进程发送的消息
ipcRenderer.on('custom-shortcut-event', (event, data) => {
    console.log('收到主进程的消息:', data.message)

    // 创建并分发自定义事件到页面
    const customEvent = new CustomEvent('myCustomEvent', {
        detail: data // 自定义事件的附加数据
    })

    // 向页面的 document 触发自定义事件
    document.dispatchEvent(customEvent)
})
// 监听主进程发送的消息
ipcRenderer.on('picture-capture-event', (event, data) => {
    console.log('收到主进程的消息:', data.message)

    // 创建并分发自定义事件到页面
    const customEvent = new CustomEvent('picture-capture-event', {
        detail: data // 自定义事件的附加数据
    })

    // 向页面的 document 触发自定义事件
    document.dispatchEvent(customEvent)
})

window.addEventListener('DOMContentLoaded', () => {
    document.addEventListener('myCustomEvent', (evt) => {
        console.log(evt, 'myCustomEvent');

    })
    document.addEventListener('myCustomEvent1', (evt) => {
        console.log(evt, 'myCustomEvent1');

    })
})



contextBridge.exposeInMainWorld('electronAPI', {
    downloadFile: (url,filename) => ipcRenderer.send('file-download', url,filename),
    createPdf: (url,filename) => ipcRenderer.send('file-create', url,filename),
    onReceive: (callback) => ipcRenderer.on('on-receive', (_event, eventName, value) => callback(eventName, value)),
    printPdf: (eventName) => ipcRenderer.send(eventName),
  })